package com.xb.loan.cashier.biz.pay.wechat.util.httpClient;

import com.xb.loan.cashier.biz.pay.wechat.WeChatConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;

/**
 发送httpsrequest请求的工具类
 */
public class HttpsRequestUtil {

    private static Logger log = LoggerFactory.getLogger(HttpsRequestUtil.class);
    
    /**
     * 发送https协议工具类
     * 
     * @param requestUrl
     * @param requestMethod
     * @param outputStr
     * @return
     * @author zheng.p
     * @date 2016年5月9日 上午11:53:40
     */
    public static String httpsRequestToString(String requestUrl, String requestMethod, String outputStr) {
        StringBuffer buffer = new StringBuffer();
        
        try {
            
            buffer = httpsRequest(requestUrl, requestMethod, outputStr);
            
        } catch (Exception e) {
            
            log.error("https请求异常：" + e.getMessage());
        }
        
        return buffer.toString();
    }

    private static StringBuffer httpsRequest(String requestUrl, String requestMethod, String output){
        
        StringBuffer buffer = new StringBuffer();
        
        HttpsURLConnection connection = null;
        OutputStream outputStream = null;
        InputStreamReader inputStreamReader = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        
        try {
            
            URL url = new URL(requestUrl);
            connection = (HttpsURLConnection) url.openConnection();
            
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod(requestMethod);
            
            if (null != output) {
                outputStream = connection.getOutputStream();
                outputStream.write(output.getBytes(WeChatConst.TRADE_CHARSET_UTF8));
                outputStream.close();
            }
            
            // 从输入流读取返回内容
            inputStream = connection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, WeChatConst.TRADE_CHARSET_UTF8);
            bufferedReader = new BufferedReader(inputStreamReader);
            
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            
        } catch (IOException e) {
            
            e.printStackTrace();
            
        }finally{
            
            //关闭读取数据流
            if(bufferedReader != null){
                
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                bufferedReader = null;
            }
            
            //关闭输入流
            if(inputStreamReader != null){
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                inputStreamReader = null;
            }
            
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                inputStream = null;
            }
            
            //关闭输入
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                outputStream = null;
            }
            
            //关闭连接
            if(connection != null){
                connection.disconnect();
            }
        }
        
        return buffer;
    }
}
